package KK.Time
{
	import KK.EMVC.Control.Control;
	import KK.GameBase.IMessageQueue;
	import KK.Time.events.DateTimeEvent;
	
	import flash.utils.getTimer;

	public class G_Date extends Control{
		
		protected var date:Date;
		protected var l_time:int;
		protected var o_time:int;
		
		static private var Instance:G_Date;
		public function G_Date(sington:Sington)
		{
			if(Instance||!sington)
			{
				throw new Error("G_Date sington class,pless using GetInstance function get Instance");
			}
			G_Date.Instance=this;
			init();
		}
		
		private function init():void{ 
			date=new Date();
			LTimer();
			
//			Time.GetInstance().setTimer(Timeout,1);
		}
		
//		private function Timeout():void{
//			trace("时间 : ",time);
//		}
//		
		//覆盖绑定事件
		override public function BindEvent(_event:IMessageQueue):Boolean{
			var result:Boolean=super.BindEvent(_event);
			initEvent();
			return result;
		}
		
		//初始化事件
		private function initEvent():void{
			AttentionMessage(DateTimeEvent.TIME_UPDATE,TimeUpdate);
		}
		
		//时间更新
		public function TimeUpdate(event:DateTimeEvent):void{
			LTimer();
			this.time=event.timer;
		}
		//设置最后运行时间
		public function LTimer():void{
			this.l_time=(getTimer()/1000);
		}
		/*
		//设置时间
		public function setTimer(value:*=null):Number{
			return date.setTime(value);
		}*/
		
		//设置日期时间
		public function set time(value:int):void{ 
			this.date.time=value-(getTimer()/1000);
		}
		
		//获取日期时间
		public function get time():int{
			return this.date.time+(getTimer()/1000);
		}
		
		
		/**
		 * 获取时间 
		 * @return 
		 * 
		 */
		static public function get Time():uint{
			return GetInstance().time;
		}
		
		/**
		 * @time 秒
		 * 转换成时间：分钟：秒 
		 * @return 
		 * 
		 */
		static public function toHHMMSS(time:uint):String{
			var result:String="";
			var hh:uint = (time/60/60%24);
			var mm:uint = (time/60%60);
			var ss:uint = (time%60);
			result = 
				(hh>10?hh:"0"+hh)+":" +//获取小时数
				(mm>10?mm:"0"+mm)+":" +// 获取分钟 
				(ss>10?hh:"0"+ss) ;//秒
			return result;
		}
		
		
		static public function GetInstance():G_Date{
			if(!G_Date.Instance)
			{
				new G_Date(new Sington); 
			}
			return G_Date.Instance;
		}
		
		
		
		
		 
	}
}


class Sington{
	
}